package netbayWatchDog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

public class WatchDog {

	public static String servletUrl = "";
	public static String TCDRecvDir = "";
	public static String JPMCRecvDir = "";
	public static String TCDRecvCmd = "";
	public static String JPMCRecvCmd = "";
	public static String UpdateDbCmd = "";
	public static Integer cycleDelay = 60;

	public static void main(String[] arg) {

		WatchDogUtility.initialize();

		boolean working = true;
		while (working) {
			try {
				checkTCDDir();
				checkJPMCDir();
				CallServlet(UpdateDbCmd);
				try {
					Thread.sleep(cycleDelay * 1000);
				} catch (InterruptedException e) {
					logging(e);
				}
			} catch (Exception e) {
				logging(e);
			}
		}

	}
	public static void logging(String str)
	{
		FileOutputStream fout;
		try {

			fout = new FileOutputStream("error.log", true);
			PrintWriter printer = new PrintWriter(fout);
			// e.printStackTrace();
			
			printer.print(new Date() + ": " + str);	
			printer.println();
			printer.close();
			fout.close();
			
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			//e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
	public static void logging(Exception e)
	{
		FileOutputStream fout;
		try {

			fout = new FileOutputStream("error.log", true);
			PrintWriter printer = new PrintWriter(fout);
			// e.printStackTrace();
			
		printer.print(new Date() + ": ");
			e.printStackTrace(printer);
			printer.println();
			printer.close();
			fout.close();
			
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			//e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}

	private static void checkTCDDir() {
//		logging("Check TCD dir");
		File TCDfolder = new File(TCDRecvDir);

		if (!TCDfolder.exists()) {
			System.out.println("The path given isn't existed");
			return;
		}

		String[] fileList = TCDfolder.list();

		if (fileList.length > 0) {

			// need to looking for ctrl file before call
			for (int i = 0; i < fileList.length; ++i) {
				if (fileList[i].length() > 6) {
					if (fileList[i].substring(fileList[i].length() - 5,
							fileList[i].length()).equalsIgnoreCase(".ctrl")) {
						// System.out.println("Found target file");
						CallServlet(TCDRecvCmd);
					}
				}
			}
		}
	}

	private static void checkJPMCDir() {
		// File TCDfolder = new File(JPMCRecvDir);
		//
		// if (!TCDfolder.exists()) {
		// System.out.println("the path given isn't existed");
		// return;
		// }
//		logging("Check jpmc dir");
		SFTPManager manager = new SFTPManager();

		if (manager.lookForNewfile()) {
			CallServlet(JPMCRecvCmd);
		}
	}

	private static void CallServlet(String cmd) {

		HttpURLConnection servletCaller = null;

		try {

			// initial connection
			String parameter = "?cmd=" + cmd;

//			System.out.println("call url " + servletUrl + parameter);
			servletCaller = (HttpURLConnection) new URL(servletUrl + parameter)
					.openConnection();
			servletCaller.setRequestMethod("GET");

			servletCaller.connect();
			servletCaller.getContent();

		} catch (MalformedURLException e) {
			logging(e);
			try {
				// sleep for 5 minute to wait until network is better
				Thread.sleep(cycleDelay * 1000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				logging(e1);
			}
		} catch (IOException e) {
			logging(e);
			try {
				// sleep for 5 minute to wait until network is better
				Thread.sleep(cycleDelay * 1000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				logging(e1);
			}
		} finally {
			if (servletCaller != null)
				servletCaller.disconnect();
		}
	}

}
